package lesson07;

import javax.swing.*;
import java.util.Scanner;

/**
 * 学生管理的主界面类
 */
public class StudentUI {
    private Scanner sc = new Scanner(System.in);
    private SuperArray array = new SuperArray();
    public void menu(){
//        System.out.println("欢迎使用学生管理系统");
        while(true){
            String input = JOptionPane.showInputDialog("欢迎使用学生管理系统\n" +
                    "请选择：1-增加 2-修改 3-删除 4-查询 5-退出");

            if("1".equals(input)){
                insert();
            }else if("2".equals(input)){
                update();
            }else if("3".equals(input)){
                delete();
            }else if("4".equals(input)){
                search();
            }else if("5".equals(input)){
                JOptionPane.showMessageDialog(null,"再见");
                System.exit(0);
            }else{
                JOptionPane.showMessageDialog(null,"输入有误，请重新输入");
            }
        }

    }
    public void insert(){
        String name = validate("请输入姓名：","^[\\u4e00-\\u9fa5]{0,}$");

        int age = 0;
        while(true){
            try {
                age = Integer.parseInt(JOptionPane.showInputDialog("请输入学生年龄："));
            }catch (NumberFormatException e){
                JOptionPane.showMessageDialog(null,"年龄必须为数字");
                continue;
            }
            break;
        }

        String gender = validate("请输入学生的性别：","^[男女]$");
        array.add(new Student(name,age,gender));
        System.out.println("增加成功");
    }
    public void update(){
        System.out.println("请输入要修改学生的姓名：");
        String name = sc.next();
        for(int i = 0;i < array.size();i++){
            if(array.get(i).equals(new Student(name))){
                System.out.println("请选择要修改的内容：1-姓名 2-年龄 3-性别");
                int input = sc.nextInt();
                Student stu = (Student) array.get(i);
                if(input == 1){
                    System.out.println("请输入修改的新名字：");
                    String newName = sc.next();
                    stu.setName(newName);
                }else if(input == 2){
                    System.out.println("请输入修改的新年龄：");
                    int newAge = sc.nextInt();
                    stu.setAge(newAge);
                }else if(input == 3){
                    System.out.println("请输入修改的新性别：");
                    String newGender = sc.next();
                    stu.setGender(newGender);
                }else{
                    System.out.println("输入有误");
                    return;
                }
                System.out.println("修改完成");
                return;
            }
        }
        System.out.println("查无此人");
    }
    public void delete(){
        System.out.println("请输入要删除学生的姓名：");
        String name = sc.next();
        for(int i = 0;i < array.size();i++){
            if(array.get(i).equals(new Student(name))){
                array.remove(i);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("查无此人");
    }
    public void search(){
        String input = JOptionPane.showInputDialog(null,"请选择：1-查询所有 2-学生姓名");
        String showStr = "";
        if("1".equals(input)){

            for(int i = 0;i < array.size();i++){
                showStr += array.get(i) + "\n";
            }
            JOptionPane.showMessageDialog(null,showStr);
        }else{
            String name = JOptionPane.showInputDialog(null,"请输入要查询的学生姓名：");
            boolean flag = false;
            for(int i = 0;i < array.size();i++){
                if(array.get(i).toString().matches("^.*"+name+".*$")){
                    showStr += array.get(i) + "\n";
                    flag = true;
                }
            }
            if(!flag){
                JOptionPane.showMessageDialog(null,"查无此人");
            }else{
                JOptionPane.showMessageDialog(null,showStr);
            }
        }
    }
    private String validate(String message,String pattern){
        String value = null;
        while(true){
            value = JOptionPane.showInputDialog(message);
            if(value.matches(pattern)){
                break;
            }
            JOptionPane.showMessageDialog(null,"格式不正确");
        }
        return value;
    }
}
